package com.zmn.plat.admin.controller.system.tool.redis;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.utils.string.StringUtil;

/**
 * @author nowind
 * @since 17/11/12 17:02
 */
@Controller
@RequestMapping("/system/tools/redis/")
public class RedisListController extends RedisBaseController {

    /**
     * 模糊查询Redis数据
     * 
     * @param request
     * @param keys
     * @return
     */
    @RequestMapping("list.action")
    public ModelAndView list(@RequestParam(value = "keys", defaultValue = "") String keys) {
        Set<String> set;
        Set<String> items = new TreeSet<>();

        int count = 0;
        if (StringUtil.isNotBlank(keys)) {
            set = redisManager.keys(keys);
            count = set.size();

            int index = 0;
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                items.add(it.next());
                index++;
                if (index >= 100) {
                    break;
                }
            }
        }

        ModelAndView mav = new ModelAndView("system/tool/redis/list");

        mav.addObject("items", items);
        mav.addObject("count", count);

        return mav;
    }

    /**
     * 查看Redis数据
     * 
     * @param request
     * @param dateType
     * @param dataId
     * @return
     */
    @RequestMapping("detail")
    public ModelAndView detail(HttpServletRequest request, @RequestParam(value = "dateType", defaultValue = "") String dateType,
        @RequestParam(value = "dataId", defaultValue = "") String dataId) {

        // 是否已经请求的标识
        String flag = "";
        if (request.getParameter("flag") != null) {
            flag = request.getParameter("flag").toString();
        }

        String data = "";
        if (StringUtil.isNotBlank(flag)) {
            if (StringUtil.isBlank(dateType)) {
                return ajaxDoneError("请选择数据类别！");
            }
            if (StringUtil.isBlank(dataId)) {
                return ajaxDoneError("请指定数据ID！");
            }

            String redisKey = dateType + dataId;
            data = redisManager.get(redisKey);

            if (StringUtil.isBlank(data)) {
                if (redisManager.exists(redisKey)) {
                    data = "数据为空";
                } else {
                    data = "没有找到数据";
                }
            }
        }

        ModelAndView mav = new ModelAndView("system/tool/redis/detail");

        mav.addObject("data", data);

        return mav;
    }

    /**
     * 查看Redis数据
     * 
     * @param request
     * @param key
     * @return
     */
    @RequestMapping("detailEdit")
    public ModelAndView detailEdit(HttpServletRequest request, @RequestParam(value = "redisKey", defaultValue = "") String key) {
        String value = "";
        Long ttl = 3600L;
        if (StringUtil.isNotBlank(key)) {

            value = redisManager.get(key);
            ttl = redisManager.ttl(key);

            if (StringUtil.isBlank(value)) {
                if (redisManager.exists(key)) {
                    value = "数据为空";
                } else {
                    value = "没有找到数据";
                }
            }
        }

        ModelAndView mav = new ModelAndView("system/tool/redis/detail_edit");

        mav.addObject("key", key);
        mav.addObject("value", value);
        mav.addObject("ttl", ttl);

        return mav;
    }
}
